package com.zline.foodraise.util;

import org.androidannotations.annotations.EBean;

import java.io.UnsupportedEncodingException;

/**
 * Created by heshantao on 15/8/10.
 * URL 地址解析
 */
@EBean
public class URLUtils {
    private static final String LOG_TAG = URLUtils.class.getSimpleName();

    //url 中获取title
    public String getUrlTitle(String url) {
        if (url.contains("?")) {
            int paramsUrl = url.indexOf("?");
            String subUrl = url.substring(paramsUrl + 1);
            if (subUrl.contains("&")) {
                String[] params = subUrl.split("&");
                for (int i = 0; i < params.length; i++) {
                    int equesIndex = params[i].indexOf("=");
                    String key = params[i].substring(0, equesIndex);
                    if (key.equals("title")) {
                        String title = params[i].substring(equesIndex + 1);
                        try {
                            title = java.net.URLDecoder.decode(title, "utf-8");
                            return title;
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "";
    }

    /*获得url 跳转的目标地址（http://mobile.line0.com/zc/product-detail.html?title=）
    希望得到product-detail
     */
    public String getUrlJumpTo(String url) {
        if (url.contains(".html?")) {
            int index = url.indexOf(".html?");
            String subUrl = url.substring(0, index);
            index = subUrl.lastIndexOf("/");
            subUrl = subUrl.substring(index + 1);
            return subUrl;
        }
        return "";
    }


    //url 中获取商品名
    public String getUrlProductName(String url) {
        if (url.contains("?")) {
            int paramsUrl = url.indexOf("?");
            String subUrl = url.substring(paramsUrl + 1);
            if (subUrl.contains("&")) {
                String[] params = subUrl.split("&");
                for (int i = 0; i < params.length; i++) {
                    int equesIndex = params[i].indexOf("=");
                    String key = params[i].substring(0, equesIndex);
                    if (key.equals("projectName")) {
                        String title = params[i].substring(equesIndex + 1);
                        try {
                            title = java.net.URLDecoder.decode(title, "utf-8");
                            return title;
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "";
    }



    //url 中获取商品Id
    public String getUrlProjectId(String url) {
        if (url.contains("?")) {
            int paramsUrl = url.indexOf("?");
            String subUrl = url.substring(paramsUrl + 1);
            if (subUrl.contains("&")) {
                String[] params = subUrl.split("&");
                for (int i = 0; i < params.length; i++) {
                    int equesIndex = params[i].indexOf("=");
                    String key = params[i].substring(0, equesIndex);
                    if (key.equals("projectId")) {
                        String title = params[i].substring(equesIndex + 1);
                        try {
                            title = java.net.URLDecoder.decode(title, "utf-8");
                            return title;
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "";
    }
}
